package com.oriental.manage.controller.specially;

import com.oriental.manage.core.paging.Pagination;
import com.oriental.manage.core.response.ResponseDTO;
import com.oriental.manage.core.utils.SessionUtils;
import com.oriental.manage.pojo.specially.BatchMessage;
import com.oriental.manage.pojo.specially.MerchantRiskInfo;
import com.oriental.manage.pojo.specially.PersonRiskInfo;
import com.oriental.manage.service.specially.BatchMessageService;
import com.oriental.manage.service.specially.MerchantRiskInfoService;
import com.oriental.manage.service.specially.PersonRiskInfoService;
import com.oriental.paycenter.commons.utils.DateUtil;
import com.oriental.paycenter.commons.utils.RandomMath;
import com.oriental.riskfront.api.enums.TransCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * Created by luoxin on 2017/4/20.
 */
@Slf4j
@Controller
@RequestMapping("riskInfo")
public class MerchantRiskInfoReportController {


    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Autowired
    private MerchantRiskInfoService merchantRiskInfoService;
    @Autowired
    private BatchMessageService batchMessageService;

    @RequestMapping("merchantRiskInfo/init")
    public String init() {

        return "specially/searchMerchantRiskInfo";
    }

    @RequestMapping("merchantRiskInfo/search")
    @ResponseBody
    public ResponseDTO<Pagination<MerchantRiskInfo>> queryPage(Pagination<MerchantRiskInfo> pagination, MerchantRiskInfo userInfo) {
        ResponseDTO<Pagination<MerchantRiskInfo>> responseDTO = new ResponseDTO<Pagination<MerchantRiskInfo>>();
        try {
            log.info("查询商户风险信息请求参数,userInfo:{}",userInfo);
            merchantRiskInfoService.queryPage(pagination, userInfo);
            responseDTO.setSuccess(true);
            log.info("查询商户风险信息返回结果,pagination:{}",pagination);
            responseDTO.setObject(pagination);
        } catch (Exception e) {
            log.error("查询失败", e);
            responseDTO.setSuccess(false);
            responseDTO.setMsg(e.getMessage());
        }
        return responseDTO;
    }

    @RequestMapping("merchantRiskInfo/toAdd")
    public String add() {
        return "specially/addMerchantRiskInfo";
    }

    @RequestMapping("merchantRiskInfo/toDetail")
    public String queryDetail() {
        return "specially/merchantRiskInfoDetail";
    }

    @RequestMapping("merchantRiskInfo/add")
    @ResponseBody
    @Transactional
    public ResponseDTO<String> addMerchantRiskInfo(@RequestBody MerchantRiskInfo merchantRiskInfo) {
        ResponseDTO<String> responseDTO = new ResponseDTO<String>();
        BatchMessage batchMessage = new BatchMessage();
        try {//Z2012931000018
            String areaCode=merchantRiskInfo.getOccurarea();
            if(areaCode.length()>1){
                String code=areaCode.substring(0,1);
                if(",".equals(code)){
                    merchantRiskInfo.setOccurarea(areaCode.substring(1));
                }
            }else{
                merchantRiskInfo.setOccurarea("");
            }
            String user = SessionUtils.getUserName();
            String Id = DateUtil.getCurrent().concat(RandomMath.getNum(5));
            merchantRiskInfo.setId(Id);
            merchantRiskInfo.setOrgId("Z2012931000018");
            merchantRiskInfo.setCusProperty("02");
            merchantRiskInfo.setRepType("03");
            merchantRiskInfo.setUpdateType("00");//00新增
            merchantRiskInfo.setStatus("00");//初始状态
            merchantRiskInfo.setRepPerson(user);
            batchMessage.setId(DateUtil.getCurrent().concat(RandomMath.getNum(5)));
            batchMessage.setSubmitMsgId(Id);
            batchMessage.setStatus("00");
            batchMessage.setDeleteFlag("0");
            batchMessage.setCount(1);
            batchMessage.setTrnxCode(TransCode.ER0001.getCode());
            batchMessage.setReportDate(new Date());
            batchMessage.setCreateBy(user);
            batchMessage.setMerchant(merchantRiskInfo.getCusCode());
            batchMessage.setRiskType(merchantRiskInfo.getRiskType());
            batchMessage.setLevel(merchantRiskInfo.getLevel());
            log.info("新增商户风险信息,merchantRiskInfo:{}",merchantRiskInfo);
            merchantRiskInfoService.insert(merchantRiskInfo, responseDTO);
            batchMessageService.insertBatchMessage(batchMessage);
        } catch (Exception e) {
            log.error("新增失败", e);
            responseDTO.setSuccess(false);
            responseDTO.setMsg(e.getMessage());
        }
        return responseDTO;
    }

}
